<?php
/**
 * @description : The DataStore of OAuth
 * @author : stephen.mo
 * @date : 2010/07/14
 */


//class a TestOAuthServer of OAuthServer
class TestOAuthServer extends OAuthServer {/*{{{*/
    public function get_signature_methods() {
        return $this->signature_methods;
    }
}/*}}}*/

//class a TVBOAuthServer of OAuthServer
class TVBOAuthServer extends OAuthServer {/*{{{*/
    public function get_signature_methods() {
        return $this->signature_methods;
    }
}/*}}}*/

//class a TVBOAuthConsumer of OAuthConsumer
class TVBOAuthConsumer extends OAuthConsumer {/*{{{*/
    //extend the father class.
}/*}}}*/

//The test code for RAS_SHA1
class TestOAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod_RSA_SHA1 {/*{{{*/
    public function fetch_private_cert(&$request) {
        $cert = <<<EOD
-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALRiMLAh9iimur8V
A7qVvdqxevEuUkW4K+2KdMXmnQbG9Aa7k7eBjK1S+0LYmVjPKlJGNXHDGuy5Fw/d
7rjVJ0BLB+ubPK8iA/Tw3hLQgXMRRGRXXCn8ikfuQfjUS1uZSatdLB81mydBETlJ
hI6GH4twrbDJCR2Bwy/XWXgqgGRzAgMBAAECgYBYWVtleUzavkbrPjy0T5FMou8H
X9u2AC2ry8vD/l7cqedtwMPp9k7TubgNFo+NGvKsl2ynyprOZR1xjQ7WgrgVB+mm
uScOM/5HVceFuGRDhYTCObE+y1kxRloNYXnx3ei1zbeYLPCHdhxRYW7T0qcynNmw
rn05/KO2RLjgQNalsQJBANeA3Q4Nugqy4QBUCEC09SqylT2K9FrrItqL2QKc9v0Z
zO2uwllCbg0dwpVuYPYXYvikNHHg+aCWF+VXsb9rpPsCQQDWR9TT4ORdzoj+Nccn
qkMsDmzt0EfNaAOwHOmVJ2RVBspPcxt5iN4HI7HNeG6U5YsFBb+/GZbgfBT3kpNG
WPTpAkBI+gFhjfJvRw38n3g/+UeAkwMI2TJQS4n8+hid0uus3/zOjDySH3XHCUno
cn1xOJAyZODBo47E+67R4jV1/gzbAkEAklJaspRPXP877NssM5nAZMU0/O/NGCZ+
3jPgDUno6WbJn5cqm8MqWhW1xGkImgRk+fkDBquiq4gPiT898jusgQJAd5Zrr6Q8
AO/0isr/3aa6O6NLQxISLKcPDk2NOccAfS/xOtfOz4sJYM3+Bs4Io9+dZGSDCA54
Lw03eHTNQghS0A==
-----END PRIVATE KEY-----
EOD;
        return $cert;
    }

    public function fetch_public_cert(&$request) {
        $cert = <<<EOD
-----BEGIN CERTIFICATE-----
MIIBpjCCAQ+gAwIBAgIBATANBgkqhkiG9w0BAQUFADAZMRcwFQYDVQQDDA5UZXN0
IFByaW5jaXBhbDAeFw03MDAxMDEwODAwMDBaFw0zODEyMzEwODAwMDBaMBkxFzAV
BgNVBAMMDlRlc3QgUHJpbmNpcGFsMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
gQC0YjCwIfYoprq/FQO6lb3asXrxLlJFuCvtinTF5p0GxvQGu5O3gYytUvtC2JlY
zypSRjVxwxrsuRcP3e641SdASwfrmzyvIgP08N4S0IFzEURkV1wp/IpH7kH41Etb
mUmrXSwfNZsnQRE5SYSOhh+LcK2wyQkdgcMv11l4KoBkcwIDAQABMA0GCSqGSIb3
DQEBBQUAA4GBAGZLPEuJ5SiJ2ryq+CmEGOXfvlTtEL2nuGtr9PewxkgnOjZpUy+d
4TvuXJbNQc8f4AMWL/tO9w0Fk80rWKp9ea8/df4qMq5qlFWlx6yOLQxumNOmECKb
WpkUQDIDJEoFUzKMVuJf4KO/FJ345+BNLGgbJ6WujreoM1X/gYfdnJ/J
-----END CERTIFICATE-----
EOD;
        return $cert;
    }
}/*}}}*/ 


/**
 * @description : TVB.COM DATA STORE
 * @author : stephen.mo
 * @date : 2010/07/07
 */
class TVBOAuthDataStore extends OAuthDataStore {/*{{{*/
    private $consumer;
    private $request_token;
    private $access_token;
    private $nonce;
    private $dbh;

    public function __construct($path=NULL) {/*{{{*/
        $this->nonce = "nonce";
    }/*}}}*/

    //lookup consumer
    public function lookup_consumer($consumer_key) {/*{{{*/
        $rv = OAuthModel::getInstance()->getConsumer('consumer',$consumer_key);
        if ($rv === FALSE) {
            return NULL;
        }

        $obj = new OAuthConsumer($rv['consumer_key'],$rv['consumer_secret']);
        if (!($obj instanceof OAuthConsumer)) {
            return NULL;
        }
        return $obj;
    }/*}}}*/
    
    //lookup token
    public function lookup_token($consumer, $token_type, $token) {/*{{{*/
        $tags = "${token_type}_${token}";
        $rv = OAuthModel::getInstance()->getToken($tags);
        if ($rv === FALSE) {
            return NULL;
        }

        //unserialized
        $obj = unserialize($rv);
        if (!($obj instanceof OAuthToken)) {
            return NULL;
        }
        return $obj;
    }/*}}}*/
    
    //lookup nonce val.
    public function lookup_nonce($consumer, $token, $nonce, $timestamp) {/*{{{*/
        $tags = "nonce_$nonce";
        //save 
        if (OAuthModel::getInstance()->queryItem($tags,'nonce')) {
            return TRUE;
        }
        else {
            OAuthModel::getInstance()->setNonce($tags);
            return FALSE;
        }
    }/*}}}*/

    //generate a new token.
    public function new_token($consumer, $type="request") {/*{{{*/
        $key = md5(time().'_'.$type);
        $secret = time() + time();
        $token = new OAuthToken($key, md5(md5($secret.'_'.$type)));
        $tags = "${type}_$key";
        $consumer_key = $consumer->key;

        //save
        if (!OAuthModel::getInstance()->setToken($consumer_key,$tags,serialize($token))) {
            throw new OAuthException("doooom!");
        }
        
        return $token;
    }/*}}}*/

    //create a new request token.
    public function new_request_token($consumer) {/*{{{*/
        return $this->new_token($consumer, "request");
    }/*}}}*/

    ///create a new access token.
    public function new_access_token($token, $consumer) {/*{{{*/
        //if allowed using cookie the service can't generate the access token again. 
        if (OAuthConfig::IS_ALLOW_COOKIE_VARS) {
            $token = OAuthModel::getInstance()->getExistsAccessToken($token->key);
            if ($token) return $token;
        }
         
        $token = $this->new_token($consumer, 'access');
        return $token;
    }/*}}}*/

}/*}}}*/


?>
